package com.bah.tract.client.ui;

import java.awt.Image;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.inject.Inject;
import javax.swing.JDialog;
import javax.xml.ws.BindingProvider;

import com.bah.tract.client.workstore.project.ProjectWork;
import com.bah.tract.client.workstore.project.ProjectWorkStore;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.microsoft.schemas.office.project.server.webservices.statusing.Statusing;
import com.microsoft.schemas.office.project.server.webservices.statusing.StatusingSoap;

@Singleton
public class ProjectSubmitDialog extends JDialog implements DataDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2479915347098051557L;

	private final ProjectTimePanel panel;

	private final ProjectWorkStore mProjectWorkStore;

	@Inject
	ProjectSubmitDialog(@Named("defaultImage") final Image image,
			final ProjectWorkStore projectWorkStore) {
		mProjectWorkStore = projectWorkStore;
		setTitle("TRACT - MS Project Time Card");
		panel = new ProjectTimePanel();
		setIconImage(image);
		add(panel);
		pack();
	}

	@Override
	public final void setVisible(final boolean b) {
		// Refresh the data in the panel if it is being displayed
		if (b) {
			refresh();
		}
		super.setVisible(b);
	}

	public class ProjectTimePanel extends javax.swing.JPanel {

		private final class JiraTimeLogger implements
				java.awt.event.ActionListener {
			@Override
			public void actionPerformed(final java.awt.event.ActionEvent evt) {

				// Post the work to Project Server
				final Set<ProjectWork> work = mProjectWorkStore.getWork();
				for (final ProjectWork pw : work) {
					StatusingSoap statusWs = new Statusing().getStatusingSoap();
					BindingProvider bp = (BindingProvider) statusWs;
					String date = new SimpleDateFormat("yyyy-MM-dd")
							.format(new Date());
					bp.getRequestContext().put(
							BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
							pw.getPwaURL() + "/_vti_bin/psi/Statusing.asmx");
					String changexml = "<Changes><Proj ID='"
							+ pw.getProjectId() + "'><Assn ID='"
							+ pw.getTaskNumber()
							+ "'><PeriodChange PID='251658250' Start='" + date
							+ "T01:00:00' End='" + date + "T23:00:00'>"
							+ pw.getMinutes() * 1000
							+ "</PeriodChange></Assn></Proj></Changes>";
					statusWs.updateStatus(changexml);
					statusWs.submitStatus(null, "");

				}

				ProjectSubmitDialog.this.setVisible(false);
			}
		}

		/**
	 * 
	 */
		private static final long serialVersionUID = 1L;

		public ProjectTimePanel() {
			initComponents();
		}

		// <editor-fold defaultstate="collapsed" desc="Generated Code">
		private void initComponents() {

			jScrollPane1 = new javax.swing.JScrollPane();
			jTable1 = new javax.swing.JTable();
			cancelBtn = new javax.swing.JButton();
			okBtn = new javax.swing.JButton();

			jScrollPane1.setViewportView(jTable1);

			cancelBtn.setText("Cancel");
			cancelBtn.addActionListener(new java.awt.event.ActionListener() {
				@Override
				public void actionPerformed(final java.awt.event.ActionEvent evt) {
					ProjectSubmitDialog.this.setVisible(false);
				}
			});

			okBtn.setText("OK");
			okBtn.addActionListener(new JiraTimeLogger());

			final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
					this);
			setLayout(layout);
			layout.setHorizontalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							layout.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.LEADING)
													.addGroup(
															javax.swing.GroupLayout.Alignment.TRAILING,
															layout.createSequentialGroup()
																	.addComponent(
																			okBtn)
																	.addPreferredGap(
																			javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																	.addComponent(
																			cancelBtn))
													.addComponent(
															jScrollPane1,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															380,
															Short.MAX_VALUE))
									.addContainerGap()));
			layout.setVerticalGroup(layout
					.createParallelGroup(
							javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
									.addContainerGap()
									.addComponent(
											jScrollPane1,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											244, Short.MAX_VALUE)
									.addPreferredGap(
											javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
									.addGroup(
											layout.createParallelGroup(
													javax.swing.GroupLayout.Alignment.BASELINE)
													.addComponent(cancelBtn)
													.addComponent(okBtn))
									.addContainerGap()));
		}// </editor-fold>

		// Variables declaration - do not modify
		private javax.swing.JButton cancelBtn;
		private javax.swing.JButton okBtn;
		private javax.swing.JScrollPane jScrollPane1;
		private javax.swing.JTable jTable1;

		// End of variables declaration

		public final void refresh() {
			final Set<ProjectWork> work = mProjectWorkStore.getWork();
			final Object[][] data = new Object[work.size()][2];
			int i = 0;
			for (final ProjectWork j : work) {
				data[i][0] = j.getLabel();
				// TODO make this pretty
				data[i][1] = j.getMinutes() + "m";
				i++;
			}
			jTable1.setModel(new javax.swing.table.DefaultTableModel(data,
					new String[] { "Task", "Effort" }));
		}
	}

	@Override
	public final void refresh() {
		panel.refresh();
	}
}
